﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Moving03.Classes;

namespace Moving03
{
    public partial class WebForm5 : BasePage
    {
        DataClasses1DataContext context = new DataClasses1DataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            string edit = Request.QueryString["edit"];
            string smh = Request.QueryString["smh"];
            
            if (edit != null && edit == "1")
            {
                if (smh != null)
                {
                    Panel2.Controls.Add(new LiteralControl("<div class='khungnho'>"));
                    Panel2.Controls.Add(new LiteralControl("<h1>"));
                    Panel2.Controls.Add(new LiteralControl("TẠO LÔ HÀNG (CẬP NHẬT)</h1>"));
                    Panel2.Controls.Add(new LiteralControl("<table style='width: 100%; color: #000000;'0' cellspacing='0' align='center'>"));
                    Panel2.Controls.Add(new LiteralControl("<tr>"));
                    Panel2.Controls.Add(new LiteralControl("<th bgcolor='#507DD2' colspan='3' style='color: #FFFFFF'>"));
                    Panel2.Controls.Add(new LiteralControl("&nbsp;1. Thêm vào thông tin chi tiết của từng mặt hàng</th>"));
                    Panel2.Controls.Add(new LiteralControl("</tr>"));
                    Panel2.Controls.Add(new LiteralControl("<tr>"));
                    Panel2.Controls.Add(new LiteralControl("<td>Tổng cộng có " + smh + " mặt hàng</td>"));
                    Panel2.Controls.Add(new LiteralControl("</tr>"));
                    Panel2.Controls.Add(new LiteralControl("<asp:ValidationSummary ID='ValidationSummary1' runat='server'"));
                    Panel2.Controls.Add(new LiteralControl(" ForeColor='#FF3300' />"));

                    for (int i = 0; i < int.Parse(smh); i++)
                    {
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<th bgcolor='#507DD2' colspan='3' style='color: #FFFFFF'>"));
                        Panel2.Controls.Add(new LiteralControl("&nbsp;" + (i + 2) + ". Mặt hàng thứ " + (i + 1) + "</th>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td colspan='3'>"));
                        Panel2.Controls.Add(new LiteralControl("<table style='width: 100%; color: #000000;' border='1'>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td style='width: 213px; color: #000000;'>"));
                        Panel2.Controls.Add(new LiteralControl("Tên mặt hàng:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("<td>"));

                        TextBox txtTenMatHang = new TextBox();
                        txtTenMatHang.ID = "txtTenMatHang" + (i + 1);
                        Panel2.Controls.Add(txtTenMatHang);
                        RequiredFieldValidator validateTenMatHang = new RequiredFieldValidator();
                        validateTenMatHang.ID = "validateTenMatHang" + (i + 1);
                        validateTenMatHang.ControlToValidate = txtTenMatHang.ID;
                        validateTenMatHang.ErrorMessage = "Tên mặt hàng không được để trống";
                        validateTenMatHang.Display = ValidatorDisplay.Dynamic;
                        validateTenMatHang.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        Panel2.Controls.Add(validateTenMatHang);

                        //Panel2.Controls.Add(new LiteralControl("<asp:TextBox ID='txtTenMatHang" + (i + 1) + "' runat='server'></asp:TextBox>"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                        Panel2.Controls.Add(new LiteralControl("Trọng lượng mặt hàng:&nbsp;&nbsp;"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("<td>"));


                        TextBox txtTrongLuong1Goi = new TextBox();
                        txtTrongLuong1Goi.ID = "txtTrongLuong1Goi" + (i + 1);
                        Panel2.Controls.Add(txtTrongLuong1Goi);
                        RequiredFieldValidator validateTrongLuong1Goi = new RequiredFieldValidator();
                        validateTrongLuong1Goi.ID = "validateTrongLuong1Goi" + (i + 1);
                        validateTrongLuong1Goi.ControlToValidate = txtTrongLuong1Goi.ID;
                        validateTrongLuong1Goi.ErrorMessage = "Trọng lượng gói hàng không được để trống";
                        validateTrongLuong1Goi.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateTrongLuong1Goi.Display = ValidatorDisplay.Dynamic;
                        Panel2.Controls.Add(validateTrongLuong1Goi);

                        RangeValidator validateTrongLuong = new RangeValidator();
                        validateTrongLuong.ID = "validateTrongLuong" + (i + 1);
                        validateTrongLuong.ControlToValidate = txtTrongLuong1Goi.ID;
                        validateTrongLuong.ErrorMessage = "Trọng lượng phải là số";
                        validateTrongLuong.Display = ValidatorDisplay.Dynamic;
                        validateTrongLuong.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateTrongLuong.MaximumValue = "999999";
                        validateTrongLuong.MinimumValue = "0";
                        Panel2.Controls.Add(validateTrongLuong);

                        Panel2.Controls.Add(new LiteralControl("<asp:TextBox ID='txtTrongLuong1Goi" + (i + 1) + "' runat='server'></asp:TextBox>"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                        Panel2.Controls.Add(new LiteralControl("Giá trị khai báo của mặt hàng: &nbsp;USD</td>"));
                        Panel2.Controls.Add(new LiteralControl("<td>"));

                        TextBox txtGiaTriUocLuong = new TextBox();
                        txtGiaTriUocLuong.ID = "txtGiaTriUocLuong" + (i + 1);
                        Panel2.Controls.Add(txtGiaTriUocLuong);
                        RequiredFieldValidator validateGiaTriUocLuong = new RequiredFieldValidator();
                        validateGiaTriUocLuong.ID = "valid" + (i + 1);
                        validateGiaTriUocLuong.ControlToValidate = txtGiaTriUocLuong.ID;
                        validateGiaTriUocLuong.ErrorMessage = "Giá trị gói hàng không được để trống";
                        validateGiaTriUocLuong.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateGiaTriUocLuong.Display = ValidatorDisplay.Dynamic;
                        Panel2.Controls.Add(validateGiaTriUocLuong);

                        RangeValidator validateGiaTri = new RangeValidator();
                        validateGiaTri.ID = "validateGiaTri" + (i + 1);
                        validateGiaTri.ControlToValidate = txtGiaTriUocLuong.ID;
                        validateGiaTri.ErrorMessage = "Giá trị phải là số";
                        validateGiaTri.Display = ValidatorDisplay.Dynamic;
                        validateGiaTri.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateGiaTri.MaximumValue = "999999";
                        validateGiaTri.MinimumValue = "0";
                        Panel2.Controls.Add(validateGiaTri);

                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("</table>"));
                        Panel2.Controls.Add(new LiteralControl("<br />"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));

                    }
                    Panel2.Controls.Add(new LiteralControl("<tr>"));
                    Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                    Panel2.Controls.Add(new LiteralControl("&nbsp</td>"));
                    Panel2.Controls.Add(new LiteralControl("<td>"));
                    string id_ttvc = Request.QueryString["idttvc"];
                    Panel2.Controls.Add(new LiteralControl("<a href='KH_CapNhatLoHang.aspx?idttvc=" + id_ttvc + "'><input type='button' value='Trở lại'/></a>"));


                    Button buttonUpdate = new Button();
                    buttonUpdate.Text = "Cập nhật hoàn tất";
                    buttonUpdate.Command += new CommandEventHandler(buttonUpdate_Click);
                    Panel2.Controls.Add(buttonUpdate);

                    Panel2.Controls.Add(new LiteralControl("</td>"));
                    Panel2.Controls.Add(new LiteralControl("</tr>"));

                    Panel2.Controls.Add(new LiteralControl("</table>"));
                    Panel2.Controls.Add(new LiteralControl("</div>"));
                }
            }
            else if (edit != null && edit == "2")
            {
                string idttvc = Request.QueryString["idttvc"];
                ThongTinVanChuyen ttvc = context.ThongTinVanChuyens.Single(i => i.Id_TTVC == int.Parse(idttvc));
                IEnumerable<MatHang> matHang = context.MatHangs.Where(i => i.Id_LoHang == ttvc.Id_LoHang);
                LoHang lh = context.LoHangs.Single(i => i.Id_LoHang == ttvc.Id_LoHang);
                if (matHang.Count() > 0)
                {
                    Panel2.Controls.Add(new LiteralControl("<div class='khungnho'>"));
                    Panel2.Controls.Add(new LiteralControl("<h1>"));
                    Panel2.Controls.Add(new LiteralControl("TẠO LÔ HÀNG (CẬP NHẬT)</h1>"));
                    Panel2.Controls.Add(new LiteralControl("<table style='width: 100%; color: #000000;'0' cellspacing='0' align='center'>"));
                    Panel2.Controls.Add(new LiteralControl("<tr>"));
                    Panel2.Controls.Add(new LiteralControl("<th bgcolor='#507DD2' colspan='3' style='color: #FFFFFF'>"));
                    Panel2.Controls.Add(new LiteralControl("&nbsp;1. Thông tin chi tiết của từng mặt hàng</th>"));
                    Panel2.Controls.Add(new LiteralControl("</tr>"));
                    Panel2.Controls.Add(new LiteralControl("<tr>"));
                    Panel2.Controls.Add(new LiteralControl("<td>Tổng cộng có " + smh + " mặt hàng</td>"));
                    Panel2.Controls.Add(new LiteralControl("</tr>"));
                    Panel2.Controls.Add(new LiteralControl("<asp:ValidationSummary ID='ValidationSummary1' runat='server'"));
                    Panel2.Controls.Add(new LiteralControl(" ForeColor='#FF3300' />"));
                    int i = 0;
                    foreach (MatHang getMatHang in matHang)
                    {
                       
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<th bgcolor='#507DD2' colspan='3' style='color: #FFFFFF'>"));
                        Panel2.Controls.Add(new LiteralControl("&nbsp;" + (i + 2) + ". Mặt hàng thứ " + (i + 1) + "</th>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td colspan='3'>"));
                        Panel2.Controls.Add(new LiteralControl("<table style='width: 100%; color: #000000;' border='1'>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td style='width: 213px; color: #000000;'>"));
                        Panel2.Controls.Add(new LiteralControl("Tên mặt hàng:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("<td>"));

                        TextBox txtTenMatHang = new TextBox();
                        txtTenMatHang.Text = getMatHang.TenMatHang;
                        txtTenMatHang.ID = "txtTenMatHang" + (i + 1);
                        Panel2.Controls.Add(txtTenMatHang);
                        RequiredFieldValidator validateTenMatHang = new RequiredFieldValidator();
                        validateTenMatHang.ID = "validateTenMatHang" + (i + 1);
                        validateTenMatHang.ControlToValidate = txtTenMatHang.ID;
                        validateTenMatHang.ErrorMessage = "Tên mặt hàng không được để trống";
                        validateTenMatHang.Display = ValidatorDisplay.Dynamic;
                        validateTenMatHang.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        Panel2.Controls.Add(validateTenMatHang);

                        //Panel2.Controls.Add(new LiteralControl("<asp:TextBox ID='txtTenMatHang" + (i + 1) + "' runat='server'></asp:TextBox>"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                        Panel2.Controls.Add(new LiteralControl("Trọng lượng mặt hàng:&nbsp;&nbsp;"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("<td>"));

                        TextBox txtTrongLuong1Goi = new TextBox();
                        txtTrongLuong1Goi.Text = getMatHang.TrongLuong.ToString();
                        txtTrongLuong1Goi.ID = "txtTrongLuong1Goi" + (i + 1);
                        Panel2.Controls.Add(txtTrongLuong1Goi);
                        RequiredFieldValidator validateTrongLuong1Goi = new RequiredFieldValidator();
                        validateTrongLuong1Goi.ID = "validateTrongLuong1Goi" + (i + 1);
                        validateTrongLuong1Goi.ControlToValidate = txtTrongLuong1Goi.ID;
                        validateTrongLuong1Goi.ErrorMessage = "Trọng lượng gói hàng không được để trống";
                        validateTrongLuong1Goi.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateTrongLuong1Goi.Display = ValidatorDisplay.Dynamic;
                        Panel2.Controls.Add(validateTrongLuong1Goi);

                        RangeValidator validateTrongLuong = new RangeValidator();
                        validateTrongLuong.ID = "validateTrongLuong" + (i + 1);
                        validateTrongLuong.ControlToValidate = txtTrongLuong1Goi.ID;
                        validateTrongLuong.ErrorMessage = "Trọng lượng phải là số";
                        validateTrongLuong.Display = ValidatorDisplay.Dynamic;
                        validateTrongLuong.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateTrongLuong.MaximumValue = "999999";
                        validateTrongLuong.MinimumValue = "0";
                        Panel2.Controls.Add(validateTrongLuong);

                        Panel2.Controls.Add(new LiteralControl("<asp:TextBox ID='txtTrongLuong1Goi" + (i + 1) + "' runat='server'></asp:TextBox>"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                        Panel2.Controls.Add(new LiteralControl("Giá trị khai báo của mặt hàng: &nbsp;USD</td>"));
                        Panel2.Controls.Add(new LiteralControl("<td>"));

                        TextBox txtGiaTriUocLuong = new TextBox();
                        txtGiaTriUocLuong.Text = getMatHang.TriGia.ToString();
                        txtGiaTriUocLuong.ID = "txtGiaTriUocLuong" + (i + 1);
                        Panel2.Controls.Add(txtGiaTriUocLuong);
                        RequiredFieldValidator validateGiaTriUocLuong = new RequiredFieldValidator();
                        validateGiaTriUocLuong.ID = "valid" + (i + 1);
                        validateGiaTriUocLuong.ControlToValidate = txtGiaTriUocLuong.ID;
                        validateGiaTriUocLuong.ErrorMessage = "Giá trị gói hàng không được để trống";
                        validateGiaTriUocLuong.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateGiaTriUocLuong.Display = ValidatorDisplay.Dynamic;
                        Panel2.Controls.Add(validateGiaTriUocLuong);

                        RangeValidator validateGiaTri = new RangeValidator();
                        validateGiaTri.ID = "validateGiaTri" + (i + 1);
                        validateGiaTri.ControlToValidate = txtGiaTriUocLuong.ID;
                        validateGiaTri.ErrorMessage = "Giá trị phải là số";
                        validateGiaTri.Display = ValidatorDisplay.Dynamic;
                        validateGiaTri.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateGiaTri.MaximumValue = "999999";
                        validateGiaTri.MinimumValue = "0";
                        Panel2.Controls.Add(validateGiaTri);

                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("</table>"));
                        Panel2.Controls.Add(new LiteralControl("<br />"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        i++;
                    }
                    Panel2.Controls.Add(new LiteralControl("<tr>"));
                    Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                    Panel2.Controls.Add(new LiteralControl("&nbsp</td>"));
                    Panel2.Controls.Add(new LiteralControl("<td>"));
                    string id_ttvc = Request.QueryString["idttvc"];
                    Panel2.Controls.Add(new LiteralControl("<a href='KH_CapNhatLoHang.aspx?idttvc=" + id_ttvc + "'><input type='button' value='Trở lại'/></a>"));

                    Button buttonUpdate = new Button();
                    buttonUpdate.Text = "Cập nhật hoàn tất";
                    buttonUpdate.Command += new CommandEventHandler(buttonUpdate_Click);
                    Panel2.Controls.Add(buttonUpdate);

                    Panel2.Controls.Add(new LiteralControl("</td>"));
                    Panel2.Controls.Add(new LiteralControl("</tr>"));

                    Panel2.Controls.Add(new LiteralControl("</table>"));
                    Panel2.Controls.Add(new LiteralControl("</div>"));
                }
                else
                {
                    Panel2.Controls.Add(new LiteralControl("<div class='khungnho'>"));
                    Panel2.Controls.Add(new LiteralControl("<h1>"));
                    Panel2.Controls.Add(new LiteralControl("TẠO LÔ HÀNG (CẬP NHẬT)</h1>"));
                    Panel2.Controls.Add(new LiteralControl("<table style='width: 100%; color: #000000;'0' cellspacing='0' align='center'>"));
                    Panel2.Controls.Add(new LiteralControl("<tr>"));
                    Panel2.Controls.Add(new LiteralControl("<th bgcolor='#507DD2' colspan='3' style='color: #FFFFFF'>"));
                    Panel2.Controls.Add(new LiteralControl("&nbsp;1. Thêm vào thông tin chi tiết của từng mặt hàng</th>"));
                    Panel2.Controls.Add(new LiteralControl("</tr>"));
                    Panel2.Controls.Add(new LiteralControl("<tr>"));
                    Panel2.Controls.Add(new LiteralControl("<td>Tổng cộng có " + smh + " mặt hàng</td>"));
                    Panel2.Controls.Add(new LiteralControl("</tr>"));
                    Panel2.Controls.Add(new LiteralControl("<asp:ValidationSummary ID='ValidationSummary1' runat='server'"));
                    Panel2.Controls.Add(new LiteralControl(" ForeColor='#FF3300' />"));

                    for (int i = 0; i < int.Parse(smh); i++)
                    {
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<th bgcolor='#507DD2' colspan='3' style='color: #FFFFFF'>"));
                        Panel2.Controls.Add(new LiteralControl("&nbsp;" + (i + 2) + ". Mặt hàng thứ " + (i + 1) + "</th>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td colspan='3'>"));
                        Panel2.Controls.Add(new LiteralControl("<table style='width: 100%; color: #000000;' border='1'>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td style='width: 213px; color: #000000;'>"));
                        Panel2.Controls.Add(new LiteralControl("Tên mặt hàng:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("<td>"));

                        TextBox txtTenMatHang = new TextBox();
                        txtTenMatHang.ID = "txtTenMatHang" + (i + 1);
                        Panel2.Controls.Add(txtTenMatHang);
                        RequiredFieldValidator validateTenMatHang = new RequiredFieldValidator();
                        validateTenMatHang.ID = "validateTenMatHang" + (i + 1);
                        validateTenMatHang.ControlToValidate = txtTenMatHang.ID;
                        validateTenMatHang.ErrorMessage = "Tên mặt hàng không được để trống";
                        validateTenMatHang.Display = ValidatorDisplay.Dynamic;
                        validateTenMatHang.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        Panel2.Controls.Add(validateTenMatHang);

                        //Panel2.Controls.Add(new LiteralControl("<asp:TextBox ID='txtTenMatHang" + (i + 1) + "' runat='server'></asp:TextBox>"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                        Panel2.Controls.Add(new LiteralControl("Trọng lượng mặt hàng:&nbsp;&nbsp;"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("<td>"));

                        TextBox txtTrongLuong1Goi = new TextBox();
                        txtTrongLuong1Goi.ID = "txtTrongLuong1Goi" + (i + 1);
                        Panel2.Controls.Add(txtTrongLuong1Goi);
                        RequiredFieldValidator validateTrongLuong1Goi = new RequiredFieldValidator();
                        validateTrongLuong1Goi.ID = "validateTrongLuong1Goi" + (i + 1);
                        validateTrongLuong1Goi.ControlToValidate = txtTrongLuong1Goi.ID;
                        validateTrongLuong1Goi.ErrorMessage = "Trọng lượng gói hàng không được để trống";
                        validateTrongLuong1Goi.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateTrongLuong1Goi.Display = ValidatorDisplay.Dynamic;
                        Panel2.Controls.Add(validateTrongLuong1Goi);

                        RangeValidator validateTrongLuong = new RangeValidator();
                        validateTrongLuong.ID = "validateTrongLuong" + (i + 1);
                        validateTrongLuong.ControlToValidate = txtTrongLuong1Goi.ID;
                        validateTrongLuong.ErrorMessage = "Trọng lượng phải là số";
                        validateTrongLuong.Display = ValidatorDisplay.Dynamic;
                        validateTrongLuong.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateTrongLuong.MaximumValue = "999999";
                        validateTrongLuong.MinimumValue = "0";
                        Panel2.Controls.Add(validateTrongLuong);

                        Panel2.Controls.Add(new LiteralControl("<asp:TextBox ID='txtTrongLuong1Goi" + (i + 1) + "' runat='server'></asp:TextBox>"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                        Panel2.Controls.Add(new LiteralControl("Giá trị khai báo của mặt hàng: &nbsp;USD</td>"));
                        Panel2.Controls.Add(new LiteralControl("<td>"));

                        TextBox txtGiaTriUocLuong = new TextBox();
                        txtGiaTriUocLuong.ID = "txtGiaTriUocLuong" + (i + 1);
                        Panel2.Controls.Add(txtGiaTriUocLuong);
                        RequiredFieldValidator validateGiaTriUocLuong = new RequiredFieldValidator();
                        validateGiaTriUocLuong.ID = "valid" + (i + 1);
                        validateGiaTriUocLuong.ControlToValidate = txtGiaTriUocLuong.ID;
                        validateGiaTriUocLuong.ErrorMessage = "Giá trị gói hàng không được để trống";
                        validateGiaTriUocLuong.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateGiaTriUocLuong.Display = ValidatorDisplay.Dynamic;
                        Panel2.Controls.Add(validateGiaTriUocLuong);

                        RangeValidator validateGiaTri = new RangeValidator();
                        validateGiaTri.ID = "validateGiaTri" + (i + 1);
                        validateGiaTri.ControlToValidate = txtGiaTriUocLuong.ID;
                        validateGiaTri.ErrorMessage = "Giá trị phải là số";
                        validateGiaTri.Display = ValidatorDisplay.Dynamic;
                        validateGiaTri.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateGiaTri.MaximumValue = "999999";
                        validateGiaTri.MinimumValue = "0";
                        Panel2.Controls.Add(validateGiaTri);

                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("</table>"));
                        Panel2.Controls.Add(new LiteralControl("<br />"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));

                    }
                    Panel2.Controls.Add(new LiteralControl("<tr>"));
                    Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                    Panel2.Controls.Add(new LiteralControl("&nbsp</td>"));
                    Panel2.Controls.Add(new LiteralControl("<td>"));
                    string id_ttvc = Request.QueryString["idttvc"];
                    Panel2.Controls.Add(new LiteralControl("<a href='KH_CapNhatLoHang.aspx?idttvc=" + id_ttvc + "'><input type='button' value='Trở lại'/></a>"));


                    Button buttonUpdate = new Button();
                    buttonUpdate.Text = "Cập nhật hoàn tất";
                    buttonUpdate.Command += new CommandEventHandler(buttonUpdate_Click);
                    Panel2.Controls.Add(buttonUpdate);

                    Panel2.Controls.Add(new LiteralControl("</td>"));
                    Panel2.Controls.Add(new LiteralControl("</tr>"));

                    Panel2.Controls.Add(new LiteralControl("</table>"));
                    Panel2.Controls.Add(new LiteralControl("</div>"));
                }
            }
            else
            {
                
                MatHang matHang = (MatHang)Session["matHang0"];
                if (matHang != null)
                {
                    Panel2.Controls.Add(new LiteralControl("<div class='khungnho'>"));
                    Panel2.Controls.Add(new LiteralControl("<h1>"));
                    Panel2.Controls.Add(new LiteralControl("TẠO LÔ HÀNG (BƯỚC 2)</h1>"));
                    Panel2.Controls.Add(new LiteralControl("<table style='width: 100%; color: #000000;'0' cellspacing='0' align='center'>"));
                    Panel2.Controls.Add(new LiteralControl("<tr>"));
                    Panel2.Controls.Add(new LiteralControl("<th bgcolor='#507DD2' colspan='3' style='color: #FFFFFF'>"));
                    Panel2.Controls.Add(new LiteralControl("&nbsp;1. Thông tin chi tiết của từng mặt hàng</th>"));
                    Panel2.Controls.Add(new LiteralControl("</tr>"));
                    Panel2.Controls.Add(new LiteralControl("<tr>"));
                    Panel2.Controls.Add(new LiteralControl("<td>Tổng cộng có " + smh + " mặt hàng</td>"));
                    Panel2.Controls.Add(new LiteralControl("</tr>"));
                    Panel2.Controls.Add(new LiteralControl("<asp:ValidationSummary ID='ValidationSummary1' runat='server'"));
                    Panel2.Controls.Add(new LiteralControl(" ForeColor='#FF3300' />"));

                    for (int i = 0; i < int.Parse(smh); i++)
                    {
                        MatHang getMatHang = (MatHang)Session["matHang" + i];
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<th bgcolor='#507DD2' colspan='3' style='color: #FFFFFF'>"));
                        Panel2.Controls.Add(new LiteralControl("&nbsp;" + (i + 2) + ". Mặt hàng thứ " + (i + 1) + "</th>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td colspan='3'>"));
                        Panel2.Controls.Add(new LiteralControl("<table style='width: 100%; color: #000000;' border='1'>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td style='width: 213px; color: #000000;'>"));
                        Panel2.Controls.Add(new LiteralControl("Tên mặt hàng:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("<td>"));

                        TextBox txtTenMatHang = new TextBox();
                        txtTenMatHang.Text = getMatHang.TenMatHang;
                        txtTenMatHang.ID = "txtTenMatHang" + (i + 1);
                        Panel2.Controls.Add(txtTenMatHang);
                        RequiredFieldValidator validateTenMatHang = new RequiredFieldValidator();
                        validateTenMatHang.ID = "validateTenMatHang" + (i + 1);
                        validateTenMatHang.ControlToValidate = txtTenMatHang.ID;
                        validateTenMatHang.ErrorMessage = "Tên mặt hàng không được để trống";
                        validateTenMatHang.Display = ValidatorDisplay.Dynamic;
                        validateTenMatHang.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        Panel2.Controls.Add(validateTenMatHang);

                        //Panel2.Controls.Add(new LiteralControl("<asp:TextBox ID='txtTenMatHang" + (i + 1) + "' runat='server'></asp:TextBox>"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                        Panel2.Controls.Add(new LiteralControl("Trọng lượng mặt hàng:&nbsp;&nbsp;"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("<td>"));

                        TextBox txtTrongLuong1Goi = new TextBox();
                        txtTrongLuong1Goi.Text = getMatHang.TrongLuong.ToString();
                        txtTrongLuong1Goi.ID = "txtTrongLuong1Goi" + (i + 1);
                        Panel2.Controls.Add(txtTrongLuong1Goi);
                        RequiredFieldValidator validateTrongLuong1Goi = new RequiredFieldValidator();
                        validateTrongLuong1Goi.ID = "validateTrongLuong1Goi" + (i + 1);
                        validateTrongLuong1Goi.ControlToValidate = txtTrongLuong1Goi.ID;
                        validateTrongLuong1Goi.ErrorMessage = "Trọng lượng gói hàng không được để trống";
                        validateTrongLuong1Goi.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateTrongLuong1Goi.Display = ValidatorDisplay.Dynamic;
                        Panel2.Controls.Add(validateTrongLuong1Goi);

                        RangeValidator validateTrongLuong = new RangeValidator();
                        validateTrongLuong.ID = "validateTrongLuong" + (i + 1);
                        validateTrongLuong.ControlToValidate = txtTrongLuong1Goi.ID;
                        validateTrongLuong.ErrorMessage = "Trọng lượng phải là số";
                        validateTrongLuong.Display = ValidatorDisplay.Dynamic;
                        validateTrongLuong.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateTrongLuong.MaximumValue = "999999";
                        validateTrongLuong.MinimumValue = "0";
                        Panel2.Controls.Add(validateTrongLuong);

                        Panel2.Controls.Add(new LiteralControl("<asp:TextBox ID='txtTrongLuong1Goi" + (i + 1) + "' runat='server'></asp:TextBox>"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                        Panel2.Controls.Add(new LiteralControl("Giá trị khai báo của mặt hàng: &nbsp;USD</td>"));
                        Panel2.Controls.Add(new LiteralControl("<td>"));

                        TextBox txtGiaTriUocLuong = new TextBox();
                        txtGiaTriUocLuong.Text = getMatHang.TriGia.ToString();
                        txtGiaTriUocLuong.ID = "txtGiaTriUocLuong" + (i + 1);
                        Panel2.Controls.Add(txtGiaTriUocLuong);
                        RequiredFieldValidator validateGiaTriUocLuong = new RequiredFieldValidator();
                        validateGiaTriUocLuong.ID = "valid" + (i + 1);
                        validateGiaTriUocLuong.ControlToValidate = txtGiaTriUocLuong.ID;
                        validateGiaTriUocLuong.ErrorMessage = "Giá trị gói hàng không được để trống";
                        validateGiaTriUocLuong.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateGiaTriUocLuong.Display = ValidatorDisplay.Dynamic;
                        Panel2.Controls.Add(validateGiaTriUocLuong);

                        RangeValidator validateGiaTri = new RangeValidator();
                        validateGiaTri.ID = "validateGiaTri" + (i + 1);
                        validateGiaTri.ControlToValidate = txtGiaTriUocLuong.ID;
                        validateGiaTri.ErrorMessage = "Giá trị phải là số";
                        validateGiaTri.Display = ValidatorDisplay.Dynamic;
                        validateGiaTri.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                        validateGiaTri.MaximumValue = "999999";
                        validateGiaTri.MinimumValue = "0";
                        Panel2.Controls.Add(validateGiaTri);

                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("</table>"));
                        Panel2.Controls.Add(new LiteralControl("<br />"));
                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));

                    }
                    Panel2.Controls.Add(new LiteralControl("<tr>"));
                    Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                    Panel2.Controls.Add(new LiteralControl("&nbsp</td>"));
                    Panel2.Controls.Add(new LiteralControl("<td>"));
                    Panel2.Controls.Add(new LiteralControl("<a href='Default.aspx'><input type='button' value='Trở lại'/></a>"));

                    Button buttonSubmit = new Button();
                    buttonSubmit.Text = "Tiếp tục";
                    buttonSubmit.Command += new CommandEventHandler(buttonSubmit_Click);
                    Panel2.Controls.Add(buttonSubmit);

                    Panel2.Controls.Add(new LiteralControl("</td>"));
                    Panel2.Controls.Add(new LiteralControl("</tr>"));

                    Panel2.Controls.Add(new LiteralControl("</table>"));
                    Panel2.Controls.Add(new LiteralControl("</div>"));
                }
                else
                {
                    if (smh != null)
                    {
                        Panel2.Controls.Add(new LiteralControl("<div class='khungnho'>"));
                        Panel2.Controls.Add(new LiteralControl("<h1>"));
                        Panel2.Controls.Add(new LiteralControl("TẠO LÔ HÀNG (BƯỚC 2)</h1>"));
                        Panel2.Controls.Add(new LiteralControl("<table style='width: 100%; color: #000000;'0' cellspacing='0' align='center'>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<th bgcolor='#507DD2' colspan='3' style='color: #FFFFFF'>"));
                        Panel2.Controls.Add(new LiteralControl("&nbsp;1. Thông tin chi tiết của từng mặt hàng</th>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td>Tổng cộng có " + smh + " mặt hàng</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));
                        Panel2.Controls.Add(new LiteralControl("<asp:ValidationSummary ID='ValidationSummary1' runat='server'"));
                        Panel2.Controls.Add(new LiteralControl(" ForeColor='#FF3300' />"));

                        for (int i = 0; i < int.Parse(smh); i++)
                        {
                            Panel2.Controls.Add(new LiteralControl("<tr>"));
                            Panel2.Controls.Add(new LiteralControl("<th bgcolor='#507DD2' colspan='3' style='color: #FFFFFF'>"));
                            Panel2.Controls.Add(new LiteralControl("&nbsp;" + (i + 2) + ". Mặt hàng thứ " + (i + 1) + "</th>"));
                            Panel2.Controls.Add(new LiteralControl("</tr>"));
                            Panel2.Controls.Add(new LiteralControl("<tr>"));
                            Panel2.Controls.Add(new LiteralControl("<td colspan='3'>"));
                            Panel2.Controls.Add(new LiteralControl("<table style='width: 100%; color: #000000;' border='1'>"));
                            Panel2.Controls.Add(new LiteralControl("<tr>"));
                            Panel2.Controls.Add(new LiteralControl("<td style='width: 213px; color: #000000;'>"));
                            Panel2.Controls.Add(new LiteralControl("Tên mặt hàng:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"));
                            Panel2.Controls.Add(new LiteralControl("</td>"));
                            Panel2.Controls.Add(new LiteralControl("<td>"));

                            TextBox txtTenMatHang = new TextBox();
                            txtTenMatHang.ID = "txtTenMatHang" + (i + 1);
                            Panel2.Controls.Add(txtTenMatHang);
                            RequiredFieldValidator validateTenMatHang = new RequiredFieldValidator();
                            validateTenMatHang.ID = "validateTenMatHang" + (i + 1);
                            validateTenMatHang.ControlToValidate = txtTenMatHang.ID;
                            validateTenMatHang.ErrorMessage = "Tên mặt hàng không được để trống";
                            validateTenMatHang.Display = ValidatorDisplay.Dynamic;
                            validateTenMatHang.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                            Panel2.Controls.Add(validateTenMatHang);


                            //Panel2.Controls.Add(new LiteralControl("<asp:TextBox ID='txtTenMatHang" + (i + 1) + "' runat='server'></asp:TextBox>"));
                            Panel2.Controls.Add(new LiteralControl("</td>"));
                            Panel2.Controls.Add(new LiteralControl("</tr>"));
                            Panel2.Controls.Add(new LiteralControl("<tr>"));
                            Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                            Panel2.Controls.Add(new LiteralControl("Trọng lượng mặt hàng:&nbsp;&nbsp;"));
                            Panel2.Controls.Add(new LiteralControl("</td>"));
                            Panel2.Controls.Add(new LiteralControl("<td>"));

                            TextBox txtTrongLuong1Goi = new TextBox();
                            txtTrongLuong1Goi.ID = "txtTrongLuong1Goi" + (i + 1);
                            Panel2.Controls.Add(txtTrongLuong1Goi);
                            RequiredFieldValidator validateTrongLuong1Goi = new RequiredFieldValidator();
                            validateTrongLuong1Goi.ID = "validateTrongLuong1Goi" + (i + 1);
                            validateTrongLuong1Goi.ControlToValidate = txtTrongLuong1Goi.ID;
                            validateTrongLuong1Goi.ErrorMessage = "Trọng lượng gói hàng không được để trống";
                            validateTrongLuong1Goi.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                            validateTrongLuong1Goi.Display = ValidatorDisplay.Dynamic;
                            Panel2.Controls.Add(validateTrongLuong1Goi);

                            RangeValidator validateTrongLuong = new RangeValidator();
                            validateTrongLuong.ID = "validateTrongLuong" + (i + 1);
                            validateTrongLuong.ControlToValidate = txtTrongLuong1Goi.ID;
                            validateTrongLuong.ErrorMessage = "Trọng lượng phải là số";
                            validateTrongLuong.Display = ValidatorDisplay.Dynamic;
                            validateTrongLuong.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                            validateTrongLuong.MaximumValue = "999999";
                            validateTrongLuong.MinimumValue = "0";
                            Panel2.Controls.Add(validateTrongLuong);

                            Panel2.Controls.Add(new LiteralControl("<asp:TextBox ID='txtTrongLuong1Goi" + (i + 1) + "' runat='server'></asp:TextBox>"));
                            Panel2.Controls.Add(new LiteralControl("</td>"));
                            Panel2.Controls.Add(new LiteralControl("</tr>"));
                            Panel2.Controls.Add(new LiteralControl("<tr>"));
                            Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                            Panel2.Controls.Add(new LiteralControl("Giá trị khai báo của mặt hàng: &nbsp;USD</td>"));
                            Panel2.Controls.Add(new LiteralControl("<td>"));

                            TextBox txtGiaTriUocLuong = new TextBox();
                            txtGiaTriUocLuong.ID = "txtGiaTriUocLuong" + (i + 1);
                            Panel2.Controls.Add(txtGiaTriUocLuong);
                            RequiredFieldValidator validateGiaTriUocLuong = new RequiredFieldValidator();
                            validateGiaTriUocLuong.ID = "valid" + (i + 1);
                            validateGiaTriUocLuong.ControlToValidate = txtGiaTriUocLuong.ID;
                            validateGiaTriUocLuong.ErrorMessage = "Giá trị gói hàng không được để trống";
                            validateGiaTriUocLuong.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                            validateGiaTriUocLuong.Display = ValidatorDisplay.Dynamic;
                            Panel2.Controls.Add(validateGiaTriUocLuong);

                            RangeValidator validateGiaTri = new RangeValidator();
                            validateGiaTri.ID = "validateGiaTri" + (i + 1);
                            validateGiaTri.ControlToValidate = txtGiaTriUocLuong.ID;
                            validateGiaTri.ErrorMessage = "Giá trị phải là số";
                            validateGiaTri.Display = ValidatorDisplay.Dynamic;
                            validateGiaTri.ForeColor = System.Drawing.ColorTranslator.FromHtml("red");
                            validateGiaTri.MaximumValue = "999999";
                            validateGiaTri.MinimumValue = "0";
                            Panel2.Controls.Add(validateGiaTri);

                            Panel2.Controls.Add(new LiteralControl("</td>"));
                            Panel2.Controls.Add(new LiteralControl("</tr>"));
                            Panel2.Controls.Add(new LiteralControl("</table>"));
                            Panel2.Controls.Add(new LiteralControl("<br />"));
                            Panel2.Controls.Add(new LiteralControl("</td>"));
                            Panel2.Controls.Add(new LiteralControl("</tr>"));

                        }
                        Panel2.Controls.Add(new LiteralControl("<tr>"));
                        Panel2.Controls.Add(new LiteralControl("<td style='width: 213px'>"));
                        Panel2.Controls.Add(new LiteralControl("&nbsp</td>"));
                        Panel2.Controls.Add(new LiteralControl("<td>"));
                        Panel2.Controls.Add(new LiteralControl("<a href='Default.aspx'><input type='button' value='Trở lại'/></a>"));


                        Button buttonSubmit = new Button();
                        buttonSubmit.Text = "Tiếp tục";
                        buttonSubmit.Command += new CommandEventHandler(buttonSubmit_Click);
                        Panel2.Controls.Add(buttonSubmit);

                        Panel2.Controls.Add(new LiteralControl("</td>"));
                        Panel2.Controls.Add(new LiteralControl("</tr>"));

                        Panel2.Controls.Add(new LiteralControl("</table>"));
                        Panel2.Controls.Add(new LiteralControl("</div>"));
                    }
                }
            }
           
        }
        private void insertMatHang(int i, int id_lohang, string tenMatHang, string trongLuong, float giaTri)
        {         
            MatHang mh = new MatHang();
            mh.Id_LoHang = id_lohang;
            mh.TenMatHang = tenMatHang;
            mh.TrongLuong = float.Parse(trongLuong);
            mh.TriGia = giaTri;
            Session["matHang"+i] = mh;
            //context.MatHangs.InsertOnSubmit(mh);
            //context.SubmitChanges();
        }
        void buttonSubmit_Click(object sender, CommandEventArgs e)
        {
            string smh = Request.QueryString["smh"];
            string idlh = Request.QueryString["idlh"];
            for (int i = 0; i < int.Parse(smh); i++)
            {
                TextBox tenMatHang;
                tenMatHang = Panel2.FindControl("txtTenMatHang" + (i + 1)) as TextBox;
                TextBox trongLuong;
                trongLuong = Panel2.FindControl("txtTrongLuong1Goi" + (i + 1)) as TextBox;
                TextBox giaTri;
                giaTri = Panel2.FindControl("txtGiaTriUocLuong" + (i + 1)) as TextBox;
                if (tenMatHang != null && trongLuong != null && giaTri != null)
                {
                    insertMatHang(i, int.Parse(idlh), tenMatHang.Text, trongLuong.Text, float.Parse(giaTri.Text));
                } 
            }
            Response.Redirect("HoanTatDatHang.aspx");
        }
        void buttonUpdate_Click(object sender, CommandEventArgs e)
        {
            string edit = Request.QueryString["edit"];
            string smh = Request.QueryString["smh"];
            string idlh = Request.QueryString["idlh"];
            if (edit != null && edit == "1")
            {
                for (int i = 0; i < int.Parse(smh); i++)
                {
                    TextBox tenMatHang;
                    tenMatHang = Panel2.FindControl("txtTenMatHang" + (i + 1)) as TextBox;
                    TextBox trongLuong;
                    trongLuong = Panel2.FindControl("txtTrongLuong1Goi" + (i + 1)) as TextBox;
                    TextBox giaTri;
                    giaTri = Panel2.FindControl("txtGiaTriUocLuong" + (i + 1)) as TextBox;
                    if (tenMatHang != null && trongLuong != null && giaTri != null)
                    {
                        MatHang mh = new MatHang();
                        mh.Id_LoHang = int.Parse(idlh);
                        mh.TenMatHang = tenMatHang.Text;
                        mh.TrongLuong = float.Parse(trongLuong.Text);
                        mh.TriGia = double.Parse(giaTri.Text);
                        context.MatHangs.InsertOnSubmit(mh);
                        context.SubmitChanges();
                    }
                }
            }
            else
            {
                IEnumerable<MatHang> matHang = context.MatHangs.Where(i => i.Id_LoHang == int.Parse(idlh));
                int j = 0;
                foreach (MatHang mh in matHang)
                {
                    TextBox tenMatHang;
                    tenMatHang = Panel2.FindControl("txtTenMatHang" + (j + 1)) as TextBox;
                    TextBox trongLuong;
                    trongLuong = Panel2.FindControl("txtTrongLuong1Goi" + (j + 1)) as TextBox;
                    TextBox giaTri;
                    giaTri = Panel2.FindControl("txtGiaTriUocLuong" + (j + 1)) as TextBox;
                    if (tenMatHang != null && trongLuong != null && giaTri != null)
                    {
                        mh.Id_LoHang = int.Parse(idlh);
                        mh.TenMatHang = tenMatHang.Text;
                        mh.TrongLuong = float.Parse(trongLuong.Text);
                        mh.TriGia = double.Parse(giaTri.Text);
                        //context.MatHangs.InsertOnSubmit(mh);
                        context.SubmitChanges();
                    }
                    j++;
                }
            }
        }
    }
}